import 'dart:convert';

import 'package:flutter_des/flutter_des.dart';

import '../public.dart';

// ignore: slash_for_doc_comments
/**
{
  "book_id":"11099",
  "cover":"",
  "book_name":"斩龙",
  "author":"通吃道人.QD",
  "introduction":"最新章节：很对不起大家！  更新日期06-10",
  "tag_name":"[恐怖科幻]"
}
    **/
class BookService {
  static Future<List<Book>> search(String searchKey, String rule,
      {int page = 1, int pageSize = 20}) async {
    String param = await FlutterDes.encryptToHex(
        "&bookName=$searchKey&reptileType=$rule&page=$page&pageSize=$pageSize",
        AppConfig.cryptKey,
        iv: AppConfig.cryptIv);
    print("searchKey $searchKey  rule $rule");
    print(
        "&bookName=$searchKey&reptileType=$rule&page=$page&pageSize=$pageSize");
    String url = AppConfig.BASE_UEL + Api.findBook + param.toString();
    List<Book> books = [];
    BaseResp res = await HttpManager().request(url);
    Map<String, dynamic> jsonObject = jsonDecode(res.data);
    books = Book.listFromJson(jsonObject);
    return books;
  }

  static Future<List<Book>> getRecommend({limit: 5}) async {
    List<Book> books = [];
    String param = await FlutterDes.encryptToHex(
        "&limit=$limit", AppConfig.cryptKey,
        iv: AppConfig.cryptIv);
    String url = AppConfig.BASE_UEL + Api.getRecommend + param.toString();
    BaseResp res = await HttpManager().request(url);
    print("res ${res.toString()}");
    if (res != null) {
      Map<String, dynamic> jsonObject = jsonDecode(res.data);
      books = Book.listFromJson2(jsonObject);
      return books;
    }
  }

  static Future<List<Book>> getRetrieval(String bookType, String bookStatus,
      {page: 1, limit: 20}) async {
    List<Book> books = [];
    print(bookStatus);
    String param = await FlutterDes.encryptToHex(
        "&book_type=$bookType"
        "&book_status=$bookStatus"
        "&page=$page"
        "&limit=$limit",
        AppConfig.cryptKey,
        iv: AppConfig.cryptIv);
    String url = AppConfig.BASE_UEL + Api.getRetrievalList + param.toString();
    BaseResp res = await HttpManager().request(url);
    print("res ${res.toString()}");
    if (res != null) {
      Map<String, dynamic> jsonObject = jsonDecode(res.data);
      books = Book.listFromJson2(jsonObject);
    }
    return books;
  }
}
